﻿using System.Text;
using Zhaoxi.DesignPattern.FlyweightPattern.Abstracts;
using Zhaoxi.DesignPattern.FlyweightPattern.Services;

namespace Zhaoxi.DesignPattern.FlyweightPattern
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到朝夕教育的体验课，我是Eleven老师");

                //{
                //    ////基础场景：准备打印个Eleven--需要用字符拼装--但是每个字符比较耗资源
                //    //AbstractWord e1 = new EWord();
                //    //AbstractWord l = new LWord();
                //    //AbstractWord e2 = new EWord();
                //    //AbstractWord v = new VWord();
                //    //AbstractWord e3 = new EWord();
                //    //AbstractWord n = new NWord();
                //    //Console.WriteLine("{0}{1}{2}{3}{4}{5}", e1.Get(), l.Get(), e2.Get(), v.Get(), e3.Get(), n.Get());
                //}
                //{
                //    ////太浪费时间和资源了，能否复用一下---通过对象复用，完成性能提升
                //    //AbstractWord e = new EWord();
                //    //AbstractWord l = new LWord();
                //    //AbstractWord v = new VWord();
                //    //AbstractWord n = new NWord();
                //    //Console.WriteLine("{0}{1}{2}{3}{4}{5}", e.Get(), l.Get(), e.Get(), v.Get(), e.Get(), n.Get());
                //    ////假如别的方法、别的类、别的线程，也需要实例化这些字符对象呢？---从全局考虑，要考虑把对象全部复用起来---
                //}
                //{
                //    //单例模式---都是同一个实例，做到了复用
                //    AbstractWord s1 = SWord.CreateInstance();
                //    AbstractWord s2 = SWord.CreateInstance();
                //    AbstractWord s3 = SWord.CreateInstance();
                //    //单例是强制保证对象唯一，还有更灵活的方式吗？--有的，享元模式
                //}
                //{
                //    ////享元模式是如何对象复用呢？
                //    //for (int i = 0; i < 5; i++)
                //    //{
                //    //    AbstractWord e = FlyweightFactory.CreateWord(FlyweightFactory.WordType.E);//new EWord();
                //    //    AbstractWord l = FlyweightFactory.CreateWord(FlyweightFactory.WordType.L);//new LWord();
                //    //    AbstractWord v = FlyweightFactory.CreateWord(FlyweightFactory.WordType.V);//new VWord();
                //    //    AbstractWord n = FlyweightFactory.CreateWord(FlyweightFactory.WordType.N);//new NWord();
                //    //    Console.WriteLine("{0}{1}{2}{3}{4}{5}", e.Get(), l.Get(), e.Get(), v.Get(), e.Get(), n.Get());
                //    //}
                //}
                //{
                //    //享元模式是如何对象复用呢？
                //    for (int i = 0; i < 5; i++)
                //    {
                //        Task.Run(() =>
                //        {
                //            AbstractWord e = FlyweightFactory.CreateWord(FlyweightFactory.WordType.E);//new EWord();
                //            AbstractWord l = FlyweightFactory.CreateWord(FlyweightFactory.WordType.L);//new LWord();
                //            AbstractWord v = FlyweightFactory.CreateWord(FlyweightFactory.WordType.V);//new VWord();
                //            AbstractWord n = FlyweightFactory.CreateWord(FlyweightFactory.WordType.N);//new NWord();
                //            Console.WriteLine("{0}{1}{2}{3}{4}{5}", e.Get(), l.Get(), e.Get(), v.Get(), e.Get(), n.Get());
                //        });
                //    }
                //    Thread.Sleep(1000 * 10);
                //    //AbstractWord e = new EWord();
                //}
                //{
                //    //享元和单例的区别？
                //    //单例是强制保证，只有一个实例
                //    //享元是基于工厂保证，其实还可以new,另外还可以多对象共享
                //}

                {
                    ////引用型变量---内存分配在进程堆上面
                    ////object.ReferenceEquals 专门比较引用连接，是否指向同一块儿内存
                    //AbstractWord e1 = new EWord();
                    //AbstractWord e2 = new EWord();
                    //AbstractWord e3 = FlyweightFactory.CreateWord(FlyweightFactory.WordType.E);
                    //AbstractWord e4 = FlyweightFactory.CreateWord(FlyweightFactory.WordType.E);

                    //Console.WriteLine($"object.ReferenceEquals(e1, e2)={object.ReferenceEquals(e1, e2)}");
                    //Console.WriteLine($"object.ReferenceEquals(e1, e3)={object.ReferenceEquals(e1, e3)}");
                    //Console.WriteLine($"object.ReferenceEquals(e1, e4)={object.ReferenceEquals(e1, e4)}");
                    //Console.WriteLine($"object.ReferenceEquals(e3, e4)={object.ReferenceEquals(e3, e4)}");
                }
                {
                    ////题目1：string类型是个引用类型--引用连接比较是否指向同一块儿内存
                    //string teacher = "Eleven";
                    //string mvp = "Eleven";
                    ////new OtherClass().Name

                    //Console.WriteLine($"object.ReferenceEquals(teacher, mvp)={object.ReferenceEquals(teacher, mvp)}");
                    //Console.WriteLine($"object.ReferenceEquals(teacher, new OtherClass().Name)={object.ReferenceEquals(teacher, new OtherClass().Name)}");
                    //Console.WriteLine($"object.ReferenceEquals(mvp, new OtherClass().Name)={object.ReferenceEquals(mvp, new OtherClass().Name)}");

                    //Console.WriteLine($"object.ReferenceEquals(new OtherClass().Name, new OtherClass().Name)={object.ReferenceEquals(new OtherClass().Name, new OtherClass().Name)}");
                    ////TTTT    TFFT

                    ////mvp = "Eleven1"; //那teacher会变吗？应该是不会的
                }
                {
                    ////题目2
                    //string teacher = "Eleven";
                    //string teacherFormat = string.Format("Ele{0}", "ven");
                    //string part = "ven";
                    //string teacherCombination = "Ele" + part;
                    //StringBuilder stringBuilder= new StringBuilder();
                    //stringBuilder.Append("Ele");
                    //stringBuilder.Append("ven");
                    //string teacherBuilder = stringBuilder.ToString();
                    //Console.WriteLine($"object.ReferenceEquals(teacher, teacherFormat)={object.ReferenceEquals(teacher, teacherFormat)}");
                    //Console.WriteLine($"object.ReferenceEquals(teacher, teacherCombination)={object.ReferenceEquals(teacher, teacherCombination)}");
                    //Console.WriteLine($"object.ReferenceEquals(teacher, teacherBuilder)={object.ReferenceEquals(teacher, teacherBuilder)}");
                    //Console.WriteLine($"object.ReferenceEquals(teacherFormat, teacherCombination)={object.ReferenceEquals(teacherFormat, teacherCombination)}");
                    //Console.WriteLine($"object.ReferenceEquals(teacherFormat, teacherBuilder)={object.ReferenceEquals(teacherFormat, teacherBuilder)}");
                }
                {
                    string teacher = "Eleven";
                    string teacherPlus = "Ele" + "ven";
                    string teacherNew = $"Ele{"ven"}";

                    Console.WriteLine($"object.ReferenceEquals(teacher, teacherPlus)={object.ReferenceEquals(teacher, teacherPlus)}");
                    Console.WriteLine($"object.ReferenceEquals(teacher, teacherNew)={object.ReferenceEquals(teacher, teacherNew)}");
                    Console.WriteLine($"object.ReferenceEquals(teacherNew, teacherPlus)={object.ReferenceEquals(teacherNew, teacherPlus)}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }


    }
}